const formatTime = (number, format = 'Y年M月D日 h:m') => {
  let formateArr = ['Y', 'M', 'D', 'h', 'm', 's']
  let returnArr = []

  var date = new Date(number)
  returnArr.push(date.getFullYear())
  returnArr.push(formatNumber(date.getMonth() + 1))
  returnArr.push(formatNumber(date.getDate()))
  returnArr.push(formatNumber(date.getHours()))
  returnArr.push(formatNumber(date.getMinutes()))
  returnArr.push(formatNumber(date.getSeconds()))

  for (const i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i])
  }
  return format
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
/** 
 * 封装的request 
 */
const request = (method = 'get', url, data = {}) => {
  let p = new Promise ((resolve, reject) => {
    wx.request({
      url: 'https://apiwisdom.yuyankeji.cn/' + url,
      data: data,
      method: method,
      success(res) {
        if (res.data.code && res.data.code == 200) {
          resolve(res.data)
        } else if (!!res.data.code) {
          wx.showToast({ title: res.data.msg + res.data.code, icon: 'none' })
          reject(res.data)
        } else {
          wx.showToast({ title: "网络请求错误!", icon: 'none' })
          reject("网络请求错误")
        }
      },
      fail(err) {
        wx.showToast({ title: "网络请求错误!", icon: 'none' })
        reject('网络请求错误')
      }
    })
  })
  return p
}

module.exports = {
  formatTime: formatTime,
  $request: request,
}
